local kuangguo = fk.CreateSkill{
  name = "wk_heg__kuangguo",
  tags = {Skill.DeputyPlace},
}

Fk:loadTranslationTable{
  ["wk_heg__kuangguo"] = "匡国",
  [":wk_heg__kuangguo"] = "副将技，此武将牌上单独的阴阳鱼个数-1；①当你杀死其他角色后，与你势力相同的角色各回复1点体力；②当你进入濒死状态被救回后，你交换主副将，然后视为使用一张【决斗】。",
  ["#wk_heg__kuangguo-choose"] = "选择一名其他角色，视为对其使用一张【决斗】",

  ["$wk_heg__kuangguo1"] = "得父荫庇，平步青云。",
  ["$wk_heg__kuangguo2"] = "吾自幼心怀父诫，方不愧父亲荫庇。",
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

-- kuangguo:addEffect(fk.Damage, {
--   anim_type = "special",
--   can_trigger = function (self, event, target, player, data)
--     if not (player:hasSkill(kuangguo.name) and H.compareKingdomWith(player, target) and player.room.current == target) then return false end
--     local events = player.room.logic:getActualDamageEvents(2, function(e)
--       return e.data.from == target
--     end, Player.HistoryTurn)
--     return #events == 1
--   end,
--   on_use = function (self, event, target, player, data)
--     if not target.dead then
--       target:drawCards(1, kuangguo.name)
--       if player ~= target then
--         player:drawCards(1, kuangguo.name)
--       end
--     end
--   end,
-- })

kuangguo:addEffect(fk.Deathed,{
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kuangguo.name) and data.damage and data.damage.from == player and target ~= player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if H.compareKingdomWith(p, player) and not p.dead then
        -- p:drawCards(1, kuangguo.name)
        room:recover({
           who = p,
           num = 1,
           recoverBy = player,
           skillName = kuangguo.name,
          })
      end
    end
  end,
})

kuangguo:addEffect(fk.AfterDying, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(kuangguo.name) and target and not target.dead and player == target
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    HG.SwapMainAndDeputy(room, player)
    -- local targets = {}
    -- local n = -1
    -- for _, p in ipairs(room.alive_players) do
    --   if H.compareKingdomWith(p, player, true) then
    --     if p:getHandcardNum() > n then
    --       targets = {p.id}
    --       n = p:getHandcardNum()
    --     elseif p:getHandcardNum() == n then
    --       table.insert(targets, p.id)
    --     end
    --   end
    -- end
    -- if #targets == 0 then return end
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1, 
      max_num = 1, 
      prompt = "#wk_heg__kuangguo-choose", 
      skill_name = kuangguo.name, 
      cancelable = false,
    })
    room:useVirtualCard("duel", nil, player, tos[1], kuangguo.name)
  end,
})

return kuangguo
